import { defineStore } from 'pinia'

export const useAuthStore = defineStore('auth', {
  state: () => ({
    token: localStorage.getItem('token') || '',
    user: JSON.parse(localStorage.getItem('user') || 'null')
  }),
  actions: {
    setAuth(token, user) {
      this.token = token || ''
      this.user = user || null
      if (token) localStorage.setItem('token', token)
      else localStorage.removeItem('token')
      if (user) localStorage.setItem('user', JSON.stringify(user))
      else localStorage.removeItem('user')
    },
    logout() {
      this.setAuth('', null)
    }
  }
})


